package GUI;


import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.SpringLayout;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import Messages.MessageAddWorkingHours;
import Messages.MessageAddWorkingHoursReply;
import Messages.MessageSavingReply;
import Messages.MessageShowAllSuppliers;
import Messages.MessageShowAllSuppliersReply;
import Messages.MessageShowAllWorkHours;
import Messages.MessageShowAllWorkHoursReply;

public class PfClock_In_Out extends JPanel {           // NOT IN USE 
	
	private MainMenu mainMenu;
	
	private JTextField textField;
	private JTextField textField_1;
	private JTextField numText;
	
	private JButton btnNewButton;
	private JButton enterButton;
	private JButton dohButton;
	private JButton exitButton;
	
	private JPanel underPanel;
	private JPanel enterPanel;
	private JPanel dohPanel;
	private JPanel exitPanel;
	private JPanel middlePanel;
	private JPanel topPanel;
	
	private Vector<String> workHours;
	private int flag;
	
	/*public PfClock_In_Out(MainMenu mm,int flagButt)
	{
		super();
		this.mainMenu=mm;
		this.flag=flagButt;
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		initialize();
		
	}*/
	
	
	
	private void initialize()
	{
		
		add(getTopPanel());
		add(getUnderPanel());
		add(getMiddlePanel());
		
	}
	
	
	//====================== panels for UnderPanel	========================================
	
	private JPanel getUnderPanel()            
	{
		
		underPanel=new JPanel();
		underPanel.setLayout(new BoxLayout(underPanel, BoxLayout.X_AXIS));
		underPanel.setBorder(new TitledBorder(null, "Under", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		underPanel.add(getEnterPanel());
		underPanel.add(getDohPanel());
		underPanel.add(getExitPanel());
		return underPanel;
	}
	
	private JPanel getEnterPanel()           
	{
				
		enterPanel=new JPanel();
		//enterPanel.setBorder(new TitledBorder(null, "Enter ", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		GridBagLayout gbll = new GridBagLayout();    
		enterPanel.setLayout(gbll);
		GridBagConstraints gbc1 = new GridBagConstraints();
		
		
		
		gbc1.gridx=0;
		gbc1.gridy=0;
		
		
		gbc1.insets = new Insets(0,0,300,0);
		
		
		
		
		
		
		enterPanel.add(getEnterButton(),gbc1);
		
		
		return enterPanel;
	}
	
	
	private JButton getEnterButton()
	{
		String num;
		enterButton = new JButton("כניסה");
		enterButton.setPreferredSize(new Dimension(150,150));
		if(flag==0)
			enterButton.setEnabled(false);
		else
			enterButton.setEnabled(true);
		
		enterButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				 workHours = new Vector<String>();
				 Date dat = new Date();
				 DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
				  DateFormat df1 = new SimpleDateFormat("HH:mm:ss");
				  DateFormat dfhh = new SimpleDateFormat("HH");
				  DateFormat dfmm = new SimpleDateFormat("mm");
				  DateFormat dfss = new SimpleDateFormat("ss");
				  String   data = df.format(dat);
				  String   time = df1.format(dat);
				  String   hours = dfhh.format(dat);
				  String   minuts = dfmm.format(dat);
				  String   seconds = dfss.format(dat);
				  System.out.println(hours);
				 workHours.add(numText.getText());
				 workHours.add(data);
				 workHours.add(time);
				 workHours.add(null);
				 workHours.add(hours);
				 workHours.add(minuts);
				 workHours.add(seconds);
				 
				MessageAddWorkingHours mess = new MessageAddWorkingHours(workHours);
				try {
					mainMenu.ms.client.sendToServer(mess);
					MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
				
			    	if (result.getResult()== '0')
			    	{
			    		JOptionPane.showMessageDialog(null,"בעיית תקשורת 03");
			    	}
			    	
			    	else 
			    	{
			    		JOptionPane.showMessageDialog(null,"הנתונים נשמרו בהצלחה");
			    		
			    	}
			    	
			    	
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			    
			  
			
			 
			 
			 
			
			}
		});
		
		return enterButton;
		
	}
	
	
	private JPanel getDohPanel()            
	{
		
		dohPanel=new JPanel();
		GridBagLayout gbl = new GridBagLayout();    
		dohPanel.setLayout(gbl);
		GridBagConstraints gbc2 = new GridBagConstraints();
		gbc2.gridx=0;
		gbc2.gridy=0;
		gbc2.insets = new Insets(0,0,300,0);
		dohPanel.add(getDohButton(),gbc2);
		
		
		
		
		return dohPanel;
	}
	
	
	private JButton getDohButton()
	{
		
		dohButton = new JButton("דוח שעות :");
		dohButton.setPreferredSize(new Dimension(100,100));
		dohButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				Date dat = new Date();
				 DateFormat dfhh = new SimpleDateFormat("MM");
				 String   months = dfhh.format(dat);
				 MessageShowAllWorkHours msgShowHours=new MessageShowAllWorkHours(numText.getText(), months);
				try {
					mainMenu.ms.client.sendToServer(msgShowHours);
					MessageShowAllWorkHoursReply resultHours=(MessageShowAllWorkHoursReply)mainMenu.ms.client.GetMessage();
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST));
					mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().add(new PfDoh_work_hours(mainMenu,resultHours.getEmployeeHours()), BorderLayout.CENTER);	
					mainMenu.getContentPane().add(new PanelFunctionsSuppliers(mainMenu), BorderLayout.EAST);
					mainMenu.getContentPane().validate();
				}
				catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
									
			}
		});
		return dohButton;
		
	}
	
	
	private JPanel getExitPanel()            
	{
		
		exitPanel=new JPanel();
		//exitPanel.setBorder(new TitledBorder(null, "Exit ", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		GridBagLayout gblll = new GridBagLayout();    
		exitPanel.setLayout(gblll);
		GridBagConstraints gbc3 = new GridBagConstraints();
		
		gbc3.gridx=0;
		gbc3.gridy=0;
		
		gbc3.insets = new Insets(0,0,300,0);
		
		
		
				
		exitPanel.add(getExitButton(),gbc3);
		
		return exitPanel;
	}
	
	
	private JButton getExitButton()
	{
		
		exitButton = new JButton("יציאה :");
		exitButton.setPreferredSize(new Dimension(150,150));
		if(flag==0)
			exitButton.setEnabled(true);
		else
			exitButton.setEnabled(false);
		exitButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				 workHours = new Vector<String>();
				 Date dat = new Date();
				 DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
				  DateFormat df1 = new SimpleDateFormat("HH:mm:ss");
				  DateFormat dfhh = new SimpleDateFormat("HH");
				  DateFormat dfmm = new SimpleDateFormat("mm");
				  DateFormat dfss = new SimpleDateFormat("ss");
				  String   data = df.format(dat);
				  String   time = df1.format(dat);
				  String   hours = dfhh.format(dat);
				  String   minuts = dfmm.format(dat);
				  String   seconds = dfss.format(dat);
				  
				 workHours.add(numText.getText());
				 workHours.add(data);
				 workHours.add(time);
				 workHours.add("1");
				 workHours.add(hours);
				 workHours.add(minuts);
				 workHours.add(seconds);
				 System.out.println(minuts);
				 
				MessageAddWorkingHours mess = new MessageAddWorkingHours(workHours);
				try {
					mainMenu.ms.client.sendToServer(mess);
					MessageSavingReply result = (MessageSavingReply)mainMenu.ms.client.GetMessage();
					
			        MessageAddWorkingHoursReply result1 = (MessageAddWorkingHoursReply)mainMenu.ms.client.GetMessage();
				
			    	if (result.getResult()== '0')
			    	{
			    		JOptionPane.showMessageDialog(null,"בעיית תקשורת.נסה שוב ");
			    	}
			    	
			    	else 
			    	{
			    					    		
			    		 AccessClockInClockOut access=new AccessClockInClockOut(mainMenu,result1.getWorkHours(),result1.getWorkMonthHours(),0);
			    	}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			  
			 
			
			}
		});
		return exitButton;
		
	}
	
	//====================== MiddlePanel	========================================
	
	
	private JPanel getMiddlePanel()           
	{
		
		
		middlePanel=new JPanel();
		middlePanel.setBorder(new TitledBorder(null, "Middle", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		middlePanel.setLayout(new SpringLayout());
		JButton okBtn = new JButton("OK");
		okBtn.setPreferredSize(new Dimension(150,50));
		middlePanel.add(okBtn);
		SpringUtilities.makeCompactGrid(middlePanel, 1, 1, //rows, cols
			     300, 0, //initX, initY
			        300, 250); //xPad, yPad
		return middlePanel;
	}
	
	
	
	
	//====================== TopPanel	========================================
	
	private JPanel getTopPanel()           
	{
		
		topPanel=new JPanel();
		topPanel.setBorder(new TitledBorder(null, "Top", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		
		return topPanel;
	}
	
}